package com.serverapk.eth;

import java.net.Inet4Address;
import java.net.InetAddress;
import android.content.Context;
//import android.net.EthernetManager;
//import android.net.IpConfiguration;
//import android.net.IpConfiguration.IpAssignment;
//import android.net.IpConfiguration.ProxySettings;
//import android.net.LinkAddress;
//import android.net.NetworkUtils;
//import android.net.StaticIpConfiguration;

public class EthSetting {
	private Context context;

	public EthSetting(Context context) {
		this.context = context;
	}

	public boolean setEthIpInfo(EthInfo ethInfo) {
//		EthernetManager mEthManager = (EthernetManager) this.context
//				.getSystemService(Context.ETHERNET_SERVICE);
//		IpAssignment mIpAssignment = mEthManager.getConfiguration()
//				.getIpAssignment();
//		if (ethInfo.isDHCP) {
//			mIpAssignment = IpAssignment.DHCP;
//			mEthManager.setConfiguration(new IpConfiguration(mIpAssignment,
//					ProxySettings.NONE, null, null));
//			return true;
//		} else {
//			mIpAssignment = IpAssignment.STATIC;
//			StaticIpConfiguration mStaticIpConfiguration = new StaticIpConfiguration();
//			int result = validateIpConfigFields(mStaticIpConfiguration,ethInfo);
//			if (result != 0) {
//				return false;
//			} else {
//				mEthManager.setConfiguration(new IpConfiguration(mIpAssignment,
//						ProxySettings.NONE, mStaticIpConfiguration, null));
//				return true;
//			}
//		}
		return true;
	}

//	private int validateIpConfigFields(
//			StaticIpConfiguration staticIpConfiguration,EthInfo ethInfo) {
//
//		String ipAddr = ethInfo.ip;
//
//		Inet4Address inetAddr = getIPv4Address(ipAddr);
//		if (inetAddr == null) {
//			return -1;
//		}
//		int networkPrefixLength = -1;
//		try {
//			networkPrefixLength = Integer.parseInt("24");
//			if (networkPrefixLength < 0 || networkPrefixLength > 32) {
//				return -1;
//			}
//			staticIpConfiguration.ipAddress = new LinkAddress(inetAddr,
//					networkPrefixLength);
//		} catch (NumberFormatException e) {
//			// Set the hint as default after user types in ip address
//			return -1;
//		}
//
//		String gateway = ethInfo.gw;
//
//		InetAddress gatewayAddr = getIPv4Address(gateway);
//		if (gatewayAddr == null) {
//			return -1;
//		}
//		staticIpConfiguration.gateway = gatewayAddr;
//
//		String dns = ethInfo.dns1;
//		InetAddress dnsAddr = null;
//
//		dnsAddr = getIPv4Address(dns);
//		if (dnsAddr == null) {
//			return -1;
//		}
//
//		staticIpConfiguration.dnsServers.add(dnsAddr);
//
//		return 0;
//	}

	private boolean isIpAddress(String value) {
		int start = 0;
		int end = value.indexOf('.');
		int numBlocks = 0;

		while (start < value.length()) {
			if (end == -1) {
				end = value.length();
			}

			try {
				int block = Integer.parseInt(value.substring(start, end));
				if ((block > 255) || (block < 0)) {
					return false;
				}
			} catch (NumberFormatException e) {
				return false;
			}

			numBlocks++;

			start = end + 1;
			end = value.indexOf('.', start);
		}
		return (numBlocks == 4);
	}

	private Inet4Address getIPv4Address(String text) {
//		try {
//			return (Inet4Address) NetworkUtils.numericToInetAddress(text);
//		} catch (Exception e) {
//			return null;
//		}
		return null;
	}

	public static class EthInfo {
		public boolean isDHCP;
		public String ip;
		public String mask;
		public String gw;
		public String dns1;
		public String dns2;
	}
}
